package cn.interestingshop.service.impl;

import cn.interestingshop.dao.BaseOrderDao;
import cn.interestingshop.dao.impl.BaseOrderDaoImpl;
import cn.interestingshop.entity.*;
import cn.interestingshop.service.BaseOrderService;
import cn.interestingshop.service.GoodsService;
import cn.interestingshop.service.OrderInfoService;

import java.util.List;

public class BaseOrderServiceImpl implements BaseOrderService {
    BaseOrderDao dao=new BaseOrderDaoImpl();
    OrderInfoService orderInfoService=new OrderInfoServiceImpl();
    GoodsService goodsService=new GoodsServiceImpl();

    @Override
    public List<BaseOrder> selectAll(int pageNo, int pageSize) {
        return dao.selectAll(pageNo,pageSize);
    }

    @Override
    public int selectCount() {
        return dao.selectCount();
    }

    @Override
    public int insert(BaseOrder baseOrder,Cart cart) {
        //1.保存订单
        int rs=dao.insert(baseOrder);
        //2.保存订单详情
        List<CartGoods> cartGoods = cart.getCartGoods();
        for (CartGoods cartGood : cartGoods) {
            OrderInfo orderInfo=new OrderInfo();
            orderInfo.setBaseOrderId(baseOrder.getId());
            orderInfo.setGoodsId(cartGood.getId());
            orderInfo.setBuyNum(cartGood.getBuyNum());
            orderInfo.setAmount(cartGood.getPrice());
            orderInfoService.insert(orderInfo);

            //3.修改商品库存
            Goods goods = goodsService.selectOne(cartGood.getId());
            goods.setStock(goods.getStock()-cartGood.getBuyNum());
            goodsService.update(goods);
        }
        return rs;
    }
}
